package org.dragonnova.meetingapp.app.adapter;

import android.content.Context;
import android.os.Build;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import org.dragonnova.meetingapp.R;
import org.dragonnova.meetingapp.app.activity.DeviceActivity;
import org.dragonnova.meetingapp.model.bean.DeviceInfo;
import org.dragonnova.meetingapp.utils.ToastUtil;

import java.util.List;

/**
 * Created by hucn on 2016/5/11.
 * Description:查找设备的界面
 */

public class DeviceAdapter extends BaseAdapter {

    private Context mContext;
    private List<DeviceInfo> deviceList;
    private boolean isEditing = false;
    private DeviceActivity mActvity;

    public DeviceAdapter(Context context, List<DeviceInfo> deviceList, DeviceActivity activity) {
        this.deviceList = deviceList;
        this.mContext = context;
        this.mActvity = activity;
    }

    @Override
    public int getCount() {
        return deviceList.size();
    }

    @Override
    public Object getItem(int position) {
        return deviceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_device, null);
            holder = new Holder();
            holder.device_tv_id = (TextView) convertView.findViewById(R.id.device_tv_id);
            holder.device_tv_name = (TextView) convertView.findViewById(R.id.device_tv_name);
            holder.device_tv_mac = (TextView) convertView.findViewById(R.id.device_tv_mac);
            holder.device_et_name = (EditText) convertView.findViewById(R.id.device_et_name);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        holder.device_tv_id.setText(deviceList.get(position).getHostId());
        holder.device_tv_name.setText(deviceList.get(position).getHostName());
        holder.device_tv_mac.setText(deviceList.get(position).getBtMacAddr());
        holder.device_et_name.setText(deviceList.get(position).getHostName());

        if (isEditing) {
            holder.device_et_name.setVisibility(View.VISIBLE);
            holder.device_tv_name.setVisibility(View.GONE);
        } else {
            holder.device_et_name.setVisibility(View.GONE);
            holder.device_tv_name.setVisibility(View.VISIBLE);
        }

        final Holder finalHolder = holder;

        finalHolder.device_et_name.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_DONE){
                    ToastUtil.showTip("正在修改，请稍候...");
                    mActvity.setName(position, finalHolder.device_et_name.getText().toString());
                    //finalHolder.device_et_name.setSelected(false);
                    return true;
                }
                return false;
            }
        });

        return convertView;
    }

    public static class Holder {
        private TextView device_tv_id;
        private TextView device_tv_mac;
        private TextView device_tv_name;
        private EditText device_et_name;
    }

    public void setIsEditable(boolean isEditing) {
        this.isEditing = isEditing;
    }
}